
// 事件工具类
// by:jackielee
// 2018.05.09
var EventUtil = {
	addHandler:function(element, type, handler){
		if(element.addEventListener){
			element.addEventListener(type, handler, false);
		}else if(element.attachEvent){
			element.attachEvent("on"+type, handler);
		}else{
			element["on"+type] = handler;
		}
	},
	getEvent:function(event){
		return event ? event : window.event;
	},
	getTarget:function(event){
		return event.target || event.srcElement;
	},
	preventDefault:function(event){
		if(event.preventDefault){
			event.preventDefault();			
		}else{
			event.returnValue = false;
		}
	},
	removeHandler:function(element, type, handler){
		if(element.removeEventListener){
			element.removeEventListener(type, handler, false);
		}else if(element.detachEvent){
			element.detachEvent("on" + type, handler);
		}else{
			element["on" + type] = null;
		}
	},
	stopPropagation:function(event){
		if(event.stopPropagation){
			event.stopPropagation();
		}else{
			event.cancelBubble = true;
		}
	},
	getRelatedTarget: function(event){
		if(event.relatedTarget){
			return event.relatedTarget;			
		}else if(event.toElement){
			return event.toElement;
		}else if(event.fromElement){
			return event.fromElement;
		}else{
			return null;
		}
	},
	getButton: function(event){
		if(document.implementation.hasFeature("MouseEvents", "2.0")){
			return event.button;
		}else{
			switch(event.button){
				case 0:
				case 1:
				case 3:
				case 5:
				case 7:
					return 0;
				case 2:
				case 6:
					return 2;
				case 4:
					return 1;
			}
		}
	},
	getWheelDelta: function(event){
		if(event.wheelDelta){
			return (client.engine.opera && client.engine.opera < 9.5 ? -event.wheelDelta : event.wheelDelta);
		}else{
			return -event.detail * 40;
		}
	},
	getCharCode: function(event){
		if(typeof event.charCode == "number" && event.charCode != 0){
			return event.charCode;
		}else{
			return event.keyCode;
		}
	},
	getClipboardText: function(event){
		var clipboardData = (event.clipboardData || window.clipboardData);
		return clipboardData.getData("text");
	},
	setClipboardText: function(event, value){
		if(event.clipboardData){
			return event.clipboardData.setData("text/plain", value);
		}else if(window.clipboardData){
			return window.clipboardData.setData("text", value);
		}
	},
};

/*
var div = document.getElementById("myDiv");
EventUtil.addHandler(div, "click", function(event){
	event = EventUtil.getEvent(event);
	var keys = new Array();
	if(event.shiftKey){
		keys.push("shift");
	}
	if(event.ctrlKey){
		keys.push("ctrl");
	}
	if(event.altKey){
		keys.push("alt");
	}
	if(event.metaKey){
		keys.push("meta");
	}
	alert("Keys:"+keys.join(","));
});
*/

/*
EventUtil.addHandler(document, "mousewheel", function(event){
	event = EventUtil.getEvent(event);
	var delta = (client.engine.opera && client.engine.opera < 9.5 ? -event.wheelDelta : event.wheelDelta);
	alert(delta);
});
*/

/*
var textbox = document.getElementById("myText");
EventUtil.addHandler(textbox, "keypress", function(event){
	event = EventUtil.getEvent(event);
	var identifier = event.key || event.keyIdentifier;
	var loc = event.location || event.keyLocation;
	if(identifier){
		alert(identifier);
	}
	if(event.getModifierState){
		alert(event.getModifierState("Shift"));
	}
});
*/

/*
<body>
	<div id="myDiv">Right click or Ctrl+click me to get a custom context menu 
		Click anywhere else to get the default context menu</div>
		<ul id="myMenu" style="position:absolution;visibility:hidden;background-color:silver">
			<li><a href="http://www.nczonline.net">Nicholas'site</a></li>
			<li><a href="http://www.wrox.com">Wrox site</a></li>
			<li><a href="http://www.yahoo.com">Yahoo!</a></li>
		</ul>
</body>

EventUtil.addHandler(window, "load", function(event){
	var div = document.getElementById("myDiv");
	EventUtil.addHandler(div, "contextmenu", function(event){
		event = EventUtil.getEvent(event);
		EventUtil.preventDefault(event);
		
		var menu = document.getElementById("myMenu");
		menu.style.left = event.clientX + "px";
		menu.style.top = event.clientY + "px";
		menu.style.visibility = "visible";
	});
	
	EventUtil.addHandler(document, "click", function(event){
		document.getElementById("myMenu").style.visibility = "hidden";
	});	
	
});*/

/*
EventUtil.addHandler(window, "beforeunload", function(event){
	event = EventUtil.getEvent(event);
	var message = "I'm really going to miss you if you go.";
	event.returnValue = message;
	return message;
	
});*/

/*
EventUtil.addHandler(document, "readystatechange", function(event){
	if(document.readyState == "interactive"){
		alert("Content loaded");
	}
});
*/

/*
EventUtil.addHandler(document, "readystatechange", function(event){
	if(document.readyState == "interactive" || document.readyState == "complete"){
		EventUtil.removeHandler(document, "readystatechange", arguments.callee);
		alert("Content loaded");
	}
});
*/

/*
EventUtil.addHandler(window, "load", function(event){
	var script = document.createElement("script");
	EventUtil.addHandler(script, "readystatechange", function(event){
		event = EventUtil.getEvent(event);
		var target = EventUtil.getTarget(event);
		
		if(target.readyState == "loaded" || target.readyState == "complete"){
			EventUtil.removeHandler(target, "readystatechange", arguments.callee);
			alert("Script Loaded");
		}
	});
});
*/
/*
EventUtil.addHandler(window, "load", function(event){
	var link = document.createElement("link");
	link.type = "text/css";
	link.rel = "stylesheet";
	
	EventUtil.addHandler(link, "readystatechange", function(event){
		event = EventUtil.getEvent(event);
		var target = EventUtil.getTarget(event);
		
		if(target.readyState == "loaded" || target.readyState == "complete"){
			EventUtil.removeHandler(target, "readystatechange", arguments.callee);
			alert("CSS Loaded");
		}
	});
});
*/

/*
(function(){
	var showCount = 0;
	
	EventUtil.addHandler(window, "load", function(){
		alert("Load fired");
	});
	EventUtil.addHandler(window, "pageshow", function(){
		showCount++;
		alert("Show has been fired "+showCount+" times.");
	});
	EventUtil.addHandler(window, "pagehide", function(){
		showCount++;
		alert("Hiding Persisted? "+event.persisted);
	});
})();
*/

/*
EventUtil.addHandler(window, "hashchange", function(event){
	alert("Old URL:"+event.oldURL+"\nNew URL:"+event.newURL);
	alert("Current hash:"+location.hash);
});

var isSupported = ("onhashchange" in window) && (document.documentMode === undefined || document.documentMode > 7);
*/
/*
EventUtil.addHandler(window, "load", function(event){
	var div = document.getElementById("myDiv");
	div.innerHTML = "Current orientation is "+window.orientation;
	
	EventUtil.addHandler(window, "orientationchange", function(event){
		div.innerHTML = "Current orientation is "+window.orientation;
	});
});
EventUtil.addHandler(window, "MozOrientation", function(event){
	var output = document.getElementById("output");
	div.innerHTML = "X="+event.x + ",Y="+event.y+",Z="+event.z+"<br>";	
});

EventUtil.addHandler(window, "deviceOrientation", function(event){
	var output = document.getElementById("output");
	div.innerHTML = "Alpha="+event.alpha+",Beta="+event.belta+",Gamma="+event.gamma+"<br>";
	
	var arrow = document.getElementById("arrow");
	arrow.style.webkitTransform = "rotate("+Math.round(event.alpha)+"deg)";
});
EventUtil.addHandler(window, "devicemotion", function(event){
	var output = document.getElementById("output");
	div.innerHTML = "Alpha="+event.rotationRate.alpha + ",Beta="+event.rotationRate.belta+",Gamma="+event.rotationRate.gamma+"<br>";	
});
*/

/*
function handleTouchEvent(event){
	// 只跟踪一次触摸
	if(event.touches.length == 1){
		var output = document.getElementById("output");
		switch(event.type){
			case "touchstart":
				outer.innerHTML = "Touch started("+event.touches[0].clientX+","+event.touches[0].clientY+")";
				break;
			case "touchend":
				outer.innerHTML = "Touch ended("+event.changedTouches[0].clientX+","+event.changedTouches[0].clientY+")";
				break;
			case "touchmove":
				event.preventDefault(); // 阻止滚动
				outer.innerHTML = "Touch moved("+event.changedTouches[0].clientX+","+event.changedTouches[0].clientY+")";
				break;
		}
	}
}
EventUtil.addHandler(document, "touchstart", handleTouchEvent);
EventUtil.addHandler(document, "touchend", handleTouchEvent);
EventUtil.addHandler(document, "touchmove", handleTouchEvent);
*/
/*
function handleGestureEvent(event){
	if(event.touches.length == 1){
		var output = document.getElementById("output");
		switch(event.type){
			case "gesturestart":
				outer.innerHTML = "Gesture started(rotation="+event.rotation+",scale="+event.scale+")";
				break;
			case "gestureend":
				outer.innerHTML = "Gesture end(rotation="+event.rotation+",scale="+event.scale+")";
				break;
			case "gesturechange":
				outer.innerHTML = "Gesture changed(rotation="+event.rotation+",scale="+event.scale+")";
				break;
		}
	}
}
document.addEventListener("gesturestart", handleGestureEvent, false);
document.addEventListener("gestureend", handleGestureEvent, false);
document.addEventListener("gesturechange", handleGestureEvent, false);
*/

/*
var list = document.getElementById("myLinks");

EventUtil.addHandler(list, function(event){
	event = EventUtil.getEvent(event);
	var target = EventUtil.getTarget(event);
	
	switch(target.id){
		case "doSomething":
			document.title = "I changed the document's title";
			break;
		case "goSomething":
			document.href = "http://www.wrox.com";
			break;
		case "sayHi":
			alert("hi");
			break;
	}
});

*/

/*
// 模拟事件

var btn = document.getElementById("myBtn");
// 创建事件对象
var event = document.createEvent("MouseEvents");
// 初始化事件对象
event.initMouseEvent("click",true,true,document.defaultView,0,0,0,0,0,false,false,false,false,0,null);
// 触发事件
btn.dispatchEvent(event);

var textbox = document.getElementById("myTextbox"),event;
// 以DOM3级方式创建事件对象
if(document.implementation.hasFeature("KeyboardEvents", "3.0")){
	event = document.createEvent("KeyboardEvents");
	
	// 初始化事件对象
	event.initKeyboardEvent("keydown",true,true,document.defaultView,"a",0,"Shift",0);
}
// 触发事件
textbox.dispatchEvent(event);

// 只适用于Firefox
var textbox = document.getElementById("myTextbox");
// 创建事件对象
var event = document.createEvent("KeyEvents");
// 初始化事件对象
event.initKeyEvent("keypress",true,true,document.defaultView,false,false,false,false,65,65);

// 触发事件
textbox.dispatchEvent(event);

// 其它浏览器
var textbox = document.getElementById("myTextbox");
// 创建事件对象
var event = document.createEvent("Events");
// 初始化事件对象
event.initEvent(type,bubbles,cancelable);
event.view = document.defaultView;
event.altKey = false;
event.ctrlKey = false;
event.shiftKey = false;
event.metaKey = false;
event.keyCode = 65;
event.charCode = 65;

// 触发事件
textbox.dispatchEvent(event);
*/

/*
// 自定义事件
var div = document.getElementById("myDiv");

EventUtil.addHandler(div, "myevent", function(event){
	alert("DIV:"+event.detail);	
});
EventUtil.addHandler(document, "myevent", function(event){
	alert("DOCUMENT:"+event.detail);
});
if(document.implementation.hasFeature("CustomEvents", "3.0")){
	event = document.createElement("CustomEvent");
	event.initCustomEvent("myevent", true,false,"Hello world!");
	div.dispatchEvent(event);
}

// IE中
var btn = document.getElementById("myBtn");
// 创建事件对象
var event = document.createEventObject();
// 初始化事件对象
event.screenX = 100;
event.screenY = 0;
event.clientX = 0;
event.clientY = 0;
event.ctrlKey = false;
event.altKey = false;
event.shiftKey = false;
event.button = 0;

// 触发事件
btn.fireEvent("onclick", event);


var textbox = document.getElementById("myTextbox");
// 创建事件对象
var event = document.createEventObject();
event.altKey = false;
event.ctrlKey = false;
event.shiftKey = false;
event.keyCode = 65;

// 触发事件
textbox.fireEvent("onkeypress", event);

*/
